package com.magic233.service;

import com.magic233.entity.Users;
import com.magic233.entity.WorkType;
import com.magic233.entity.Workers;
import com.magic233.mapper.WorkTypeMapper;
import com.magic233.mapper.WorkersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * ClassName: WorkTypeImpl
 * Description:
 * date: 2021/5/30 14:23
 * author: zouyuan
 */
@Service
public class WorkTypeServiceImpl implements WorkTypeService {
    @Autowired
    private WorkTypeMapper workTypeMapper;

    @Autowired
    private LogsService logsService;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Override
    public int addWorkType(Users user, String type) {
        //查找是否已经存在该type
        WorkType workType = workTypeMapper.selectByType(type);
        if(workType == null){
            //没有该类别
            Long id = new Date().getTime();
            workType = new com.magic233.entity.WorkType();
            workType.setType(type);
            workType.setCreatetime(simpleDateFormat.format(id));
            workType.setCreateid(user.getId());
            workType.setDeleted(0);

            int re = workTypeMapper.insert(workType);
            if (re == 1) {
                String thing = "超级管理员：{" + user.getPhone() + "--" + user.getUsername() + "}新增了维修类型：" + type;
                logsService.addLog(thing);
            }
            return re;
        }else {
            //被逻辑删除的
            if(workType.getDeleted() == 1){
                workType.setCreatetime(simpleDateFormat.format(new Date().getTime()));
                workType.setCreateid(user.getId());
                workType.setDeleted(0);
                int re = workTypeMapper.updateByPrimaryKey(workType);
                if (re == 1) {
                    String thing = "超级管理员：{" + user.getPhone() + "--" + user.getUsername() + "}新增了维修类型：" + type;
                    logsService.addLog(thing);
                }
                return re;
            }else {
                //已经存在
                return 0;
            }
        }
    }

    @Override
    public int deleteWorkType(Users user, String type) {
        int re = workTypeMapper.deleteByType(type);
        if (re == 1) {
            String thing = "超级管理员：{" + user.getPhone() + "--" + user.getUsername() + "}删除了维修类型：" + type;
            logsService.addLog(thing);
        }
        return re;
    }

    @Override
    public List<WorkType> selectWorkType() {
        List<WorkType> workTypes = workTypeMapper.selectAll();
        return workTypes;
    }
}
